<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never" v-loading="loading">
        <!-- 搜索栏 -->
        <el-form
          :inline="true"
          :model="pagination"
          ref="pagination"
        >
          <el-form-item label="配件商：" prop="businessName">
            <el-input v-model="pagination.storeName" placeholder="请输入配件商"></el-input>
          </el-form-item>
          <el-form-item label="配件编码：" prop="code">
            <el-input v-model="pagination.code" placeholder="请输入配件编码"></el-input>
          </el-form-item>
          <el-form-item label="配件名称：" prop="componentName">
            <el-input v-model="pagination.componentName" placeholder="请输入配件名称"></el-input>
          </el-form-item>
          <el-form-item label="规格型号：" prop="specification">
            <el-input v-model="pagination.specification" placeholder="请输入规格型号"></el-input>
          </el-form-item>
<!--          <el-form-item label="配件品牌：" prop="brand">-->
<!--            <el-input v-model="pagination.brand" placeholder="请输入品牌名称"></el-input>-->
<!--          </el-form-item>-->
          <el-form-item label="分类名称：" prop="typeName">
            <el-input v-model="pagination.componentTypeName" placeholder="请输入分类名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="handleRest">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 搜索栏 / -->
        <div class="btnBox" style="text-align:right">
          <el-button :type="pagination.state == 1 ? 'primary' : ''" @click="stateHandle">已下架商品</el-button>
        </div>
        <!-- 数据表格 -->
        <el-table
          :data="items"
          border
          style="width: 100%; margin-top:10px;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="index" label="#" width="55"></el-table-column>
          <el-table-column prop="store.code" label="配件商编码"></el-table-column>
          <el-table-column prop="store.name" label="配件商"></el-table-column>
          <el-table-column prop="code" label="配件编码"></el-table-column>
          <el-table-column prop="componentName" label="配件名称"></el-table-column>
          <el-table-column prop="specification" label="规格型号"></el-table-column>
          <el-table-column prop="componentType.name" label="分类名称"></el-table-column>
          <el-table-column prop="componentBrand.brandName" label="配件品牌"></el-table-column>
          <el-table-column prop="price" label="价格"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
<!--              <el-button @click="handleEdit(scope.row)" type="text" size="small">修改</el-button>-->
              <el-button @click="handleDelect(scope.row)" type="text" size="small">
                {{scope.row.state == 1 ? '上架' : '下架'}}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Number(total)"
        ></el-pagination>
        <!-- 数据表格 / -->
      </el-card>
      <!-- 新增标签弹层 -->
      <Dialog ref="addType" :dialogTitle="dialogTitle" v-on:newDataes = subMitForm></Dialog>
    </div>
  </div>
</template>

<script>
import { getList, getDetails, updateData } from '@/api/component'
import Dialog from './../components/componentAdd'
export default {
  name: 'componentList',
  components: {
    Dialog
  },
  data() {
    return {
      items: [
        {
        }
      ],
      total: 20,
      pagination: {
        page: 1,
        pageSize: 10
      },
      loading: false,
      multipleSelection: [],
      dialogVisible: false,
      dialogTitle: ''
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    // 业务方法
    async doQuery(parent) {
      this.loading = true
      // this.items = []
      await getList(parent)
        .then(res => {
          const { data } = res
          this.items = data.items
          this.total = data.counts
          this.loading = false
        })
        .catch(err => {
          console.log(err)
          this.loading = false
        })
    },
    stateHandle() {
      if (this.pagination.state === '') {
        this.pagination.state = 1
        this.doQuery(this.pagination)
      } else {
        this.pagination.state = ''
        this.doQuery(this.pagination)
      }

    },
    // UI方法 - 重置
    handleRest() {
      this.$refs['pagination'].resetFields()
      this.pagination.storeName = ''
      this.pagination.componentTypeName = ''
    },
    // 搜索
    handleSearch() {
      this.doQuery(this.pagination)
    },
    //
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    // 每页条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pagination.pageSize = val
      this.doQuery(this.pagination)
    },
    // 当前页
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pagination.page = val
      this.doQuery(this.pagination)
    },
    // 添加
    handleAdd(val) {
      this.dialogTitle = '添加'
      this.$refs.addType.dialogFormV()
    },
    // 详情获取  - 如果有详情接口 读详情
    async getDetails(id) {
      let dataes = {}
      await getDetails(id)
        .then(res => {
          const { data } = res
          this.loading = false
          dataes = data
        })
        .catch(err => {
          console.log(err)
          this.loading = false
        })
      return dataes
    },
    // 编辑
    async handleEdit (val) {
      this.dialogTitle = '编辑'
      // 如果有详情接口 读详情
      const data = await this.getDetails(val.id)
      // 如果列表中的数据可以满足修改的信息展示  则不用读取上面的详情接口 用下面的data
      // const data = val
      this.$refs.addType.dialogFormV(data)
    },
    // 删除
    handleDelect(item) {
      this.$confirm('是否执行更改状态操作？')
        .then(async ret => {
          await updateData({id: item.id, state: item.state === 1 ? 0 : 1})
            .then(res => {
              this.loading = false
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.doQuery(this.pagination)
            })
            .catch(err => {
              console.log(err)
              this.loading = false
            })
        })
        .catch(ret => {
          console.log(ret)
        })
    },
    // subMitForm
    subMitForm(val) {
      if (val.id) {
        this.$message.success('修改成功')
      } else {
        this.$message.success('添加成功')
      }
      this.doQuery(this.pagination)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px 0px 0px;
}
.pagination {
  margin-top: 10px;
  text-align: right;
}
</style>
